# -*- coding:utf-8 -*-
import time

reps = 1000
nums = 20000


def basefun(x):
    return {"key": x}


def getTestTime(fun, *args):
    start = time.time()
    for i in range(reps):
        fun(*args)
    costtime = time.time() - start
    return costtime


def fun_While():
    res = []
    x = 0
    while x < nums:
        res.append(basefun(x))
        x += 1
    return res


def fun_for():
    res = []
    for i in range(nums):
        res.append(basefun(i))
    return res


def fun_expression():  # 生成器
    res = list(basefun(x) for x in range(nums))
    return res


def fun_comprehension():  # 列表解析
    res = [basefun(x) for x in range(nums)]
    return res


def fun_map():  # map
    res = map(basefun, range(nums))
    return res


time_while = getTestTime(fun_While)
print "While 花费时间 %s s" % time_while
time_for = getTestTime(fun_for)
print "for 花费时间 %s s" % time_for
time_expression = getTestTime(fun_expression)
print "expression 花费时间 %s s" % time_expression
time_comprehension = getTestTime(fun_comprehension)
print "comprehenssion 花费时间 %s s" % time_comprehension
time_map = getTestTime(fun_map)
print "map 花费时间 %s s" % time_map
